<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-alert title="完整编号(10位)=设施类型(2位)+路段类型(1位)+杆件编号(5位)+子编号(2位)，必须按照真实情况输入准确的设备所在位置，保证数据准确性。" :closable="false" type="success"/>
    <BR/>

    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >  
    

    <el-form-item label="设施类型" prop="deviceType">
        <el-select v-model="formData.deviceType" placeholder="请选择设施类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.DEVICE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="路段类型" prop="roadType">
        <el-select v-model="formData.roadType" placeholder="请选择设施所在的路段类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ROAD_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="杆件编号" prop="memCode">
        <el-input v-model="formData.memCode" placeholder="请输入杆件编号(00000-99999)" />
      </el-form-item>
      <el-form-item label="子编号" prop="subCode">
        <el-input v-model="formData.subCode"  placeholder="请输入子编号(当一个杆件下有多个设备时用子编号来表示)" />
      </el-form-item>
      <el-form-item label="完整编号" prop="deviceCode">
        <el-input v-model="formData.deviceCode" :disabled="true"  placeholder="完整编号(10位)=设施类型(2位)+路段类型(1位)+杆件编号(5位)+子编号(2位)" />
      </el-form-item>

      <el-form-item label="路口名" prop="roadName">
        <el-input v-model="formData.roadName" placeholder="请输入路口名" />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="formData.deviceName" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item label="一机一档" prop="distCode">
        <el-input v-model="formData.distCode" placeholder="请输入一机一档" />
      </el-form-item>
      <el-form-item label="纬度" prop="lat">
        <el-input v-model="formData.lat" placeholder="请输入纬度" />
      </el-form-item>
      <el-form-item label="经度" prop="lng">
        <el-input v-model="formData.lng" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item> 

     
     
     
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { DataApi, DataVO } from '@/api/device/devicedata'
import { ref,watch,reactive, watchEffect , nextTick} from 'vue';
/** 设备台账 表单 */
defineOptions({ name: 'DataForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  roadName: undefined,
  deviceName: undefined,
  distCode: undefined,
  lat: undefined,
  lng: undefined,
  remark: undefined,
  creator: undefined,
  updater: undefined,
  deviceCode: '',
  subCode: '',
  deviceType: '',
  roadType: '',
  memCode: ''
})

 

watchEffect(() => {
  formData.value.deviceCode = formData.value.deviceType 
  formData.value.deviceCode =  formData.value.deviceCode + formData.value.memCode 
  formData.value.deviceCode =  formData.value.deviceCode + formData.value.subCode  
    });

 

const formRules = reactive({
  roadName: [{ required: true, message: '路口名不能为空', trigger: 'blur' }],
  deviceName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
  deviceCode: [{ required: true, message: '完整编号不能为空', trigger: 'blur' }],
  subCode: [{ required: true,  message: '子编号不能为空', trigger: 'blur' },
  { min:2, max:2, message: '子编号只能2位', trigger: 'blur' }
  ],
  deviceType: [{ required: true, message: '设施类型不能为空', trigger: 'change' }],
  roadType: [{ required: true, message: '路段类型不能为空', trigger: 'change' }],
  memCode: [{ required: true, message: '杆件编号不能为空', trigger: 'blur' },
  { min:5, max:5, message: '杆件编号只能5位', trigger: 'blur' }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type )
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DataApi.getData(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DataVO
    if (formType.value === 'create') {
      await DataApi.createData(data)
      message.success(t('common.createSuccess'))
    } else {
      await DataApi.updateData(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    roadName: undefined,
    deviceName: undefined,
    distCode: undefined,
    lat: undefined,
    lng: undefined,
    remark: undefined,
    creator: undefined,
    updater: undefined,
    deviceCode: "",
    subCode: "",
    deviceType: "",
    roadType: "",
    memCode: ""
  }
  formRef.value?.resetFields()
}
</script>